package com.tajiang.business.mess.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.adapter.EmptyViewAdapter;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;
import com.tajiang.business.common.utils.DensityUtils;
import com.tajiang.business.common.widget.LoadingDialog;
import com.tajiang.business.mess.adapter.FoodMangementAdapter;
import com.tajiang.business.model.FoodManageMent;
import com.tajiang.business.model.Pager;
import com.tajiang.business.order.view.OrderLoadMoreFooterView;
import com.tajiang.business.order.view.OrderRefreshHeadView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admins on 2016/10/10.
 */
public class FoodManagementActivity extends BaseActivity
                                    implements HttpResponseListener, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_refresh_header)
    OrderRefreshHeadView swipeRefreshHeader;
    @BindView(R.id.swipe_target)
    RecyclerView rvFoodMenuList;
    @BindView(R.id.swipe_load_more_footer)
    OrderLoadMoreFooterView swipeLoadMoreFooter;
    @BindView(R.id.swipe_to_load_layout)
    SwipeToLoadLayout swipeToLoadLayout;

    private LoadingDialog  loadingDialog;
    private static final int DEFAULT_FIRST_PAGE = 1;  //首次加载首页当前Page
    private int currentPage  = DEFAULT_FIRST_PAGE;

    private FoodMangementAdapter mFoodMangementAdapter;
    private EmptyViewAdapter mEmptyViewAdapter;  //列表为空的时候显示空Adapter

    @Override
    protected void initTopBar() {
        setTitle("菜品管理");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_food_management);
        initMyListener();
    }

    private void initMyListener() {
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
//        initAdapterData();
        loadingDialog = new LoadingDialog(this);
        mFoodMangementAdapter = new FoodMangementAdapter(this, new ArrayList<FoodManageMent>());
        mEmptyViewAdapter = new EmptyViewAdapter(this, R.layout.layout_empty_mess_food_view);
        rvFoodMenuList.setLayoutManager(new LinearLayoutManager(this));
        rvFoodMenuList.setAdapter(mEmptyViewAdapter);
        rvFoodMenuList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .margin(DensityUtils.dp2px(this, 8), DensityUtils.dp2px(this, 8))
                .color(getResources().getColor(R.color.driver)).size(2).build());

        initHttpRequest();
    }

    private void initHttpRequest() {
        loadingDialog.show();
        swipeToLoadLayout.setLoadMoreEnabled(false);
        new HttpHandler(this).getBusinessGoods(DEFAULT_FIRST_PAGE, 10);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore() {

        new HttpHandler(new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                Pager<FoodManageMent> pager = (Pager<FoodManageMent>) response.getData();
                List<FoodManageMent> newOrderList = pager.getList();

                if (newOrderList != null && newOrderList.size() > 0) {
                    currentPage = currentPage + 1;
                    mFoodMangementAdapter.addAllAndUpdateDataSet(newOrderList);
                    swipeLoadMoreFooter.setFootTextView("加载完毕！");
                } else {
                    swipeLoadMoreFooter.setFootTextView("已经到底咯！");
                }
            }

            @Override
            public void onFailed(BaseResponse response) {

            }

            @Override
            public void onFinish() {
                if (swipeToLoadLayout.isLoadingMore()) {
                    swipeToLoadLayout.setLoadingMore(false);
                }
            }
        }).getBusinessGoods(currentPage + 1,10);
    }

    @Override
    public void onRefresh() {
        this.currentPage = DEFAULT_FIRST_PAGE;
        new HttpHandler(this).getBusinessGoods(DEFAULT_FIRST_PAGE, 10);
    }

    @Override
    public void onSuccess(BaseResponse response) {
        Pager<FoodManageMent> pager = (Pager<FoodManageMent>) response.getData();
        List<FoodManageMent> dataList = pager.getList();

        if (dataList == null || dataList.size() < 1) {
            swipeToLoadLayout.setLoadMoreEnabled(false);
            rvFoodMenuList.setAdapter(mEmptyViewAdapter);
        } else {
            swipeToLoadLayout.setLoadMoreEnabled(true);
            rvFoodMenuList.setAdapter(mFoodMangementAdapter);
            mFoodMangementAdapter.updateAllDataSet(dataList);
        }
    }

    @Override
    public void onFailed(BaseResponse response) {

    }

    @Override
    public void onFinish() {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        if (swipeToLoadLayout.isRefreshing()) {
            swipeToLoadLayout.setRefreshing(false);
        }
    }

    @Override
    public void onBackPressed() {
        FoodManagementActivity.this.finish();
    }
}
